<?php

// include the PHP Facebook Client Library
require_once('../facebook/facebook.php');
require_once('db.php');

// application class to fetch application data
include_once('application.php'); 

// display the tabs
include_once('tabs.php');

$facebook = new Facebook(Application::$apiKey, Application::$apiSecret);
$user_id = $facebook->require_login();

if(isset($_POST['submit'])) 
{
 // Check the input for correctness
 if($_POST['friendName'] == '' || 
   $_POST['friendUid'] == '' || 
   $_POST['bioText'] == '')
 {
  // raise a dialog here
  header(index.php);
  }
 else
 {
  // Establish a connection with the DB server and select a DB 
  $db = new DB(Application::$mysqlServer, Application::$mysqlUserName, Application::$mysqlPassword);
  $db->selectDatabase(Application::$dbName);

  // Insert a new entry, a new bio default to unapproved status
  $record = sprintf("%u, %u, '%s', '%s', %d, %d",
    $_POST['fb_sig_user'],
    $_POST['friendUid'],
    mysql_real_escape_string($_POST['bioText']),
    date("Y-m-d"),
    0, // Default to unapproved
    NULL // Set sort order to NULL
    );

  // $db->insertValues(Application::$tableName, $record);
  
  // Send a notification to the user
  $message = sprintf("%s received a bio from %s.", $_POST['friendUid'], $_POST['fb_sig_user']);
  echo $message;
  $successList = $facebook->api_client->notifications_sendEmail($_POST['friendUid'], 'message', 'user_to_user');
  echo $successList;
  }
}
else if(isset($_POST['cancel'])) 
{
 header(index.php);
}


?>

<br>Write something cool about your friend and tell the world how great he/she is
<!-- Draw the submit form in FBML -->
<fb:editor action="index.php" >
<fb:friend-selector idname="friendUid" name="friendName" />
<fb:editor-textarea label="Type the bio here" name="bioText" />
<fb:editor-buttonset>
<fb:editor-button value="Submit" name="submit" />
<fb:editor-button value="Cancel" name="cancel" />
</fb:editor-buttonset>
</fb:editor>

